package com.demo.common.utils;

import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * 读取属性，属性文件固定在/resources/property下面
 */
public abstract class PropertyUtils {

    /**
     * 读取属性
     * @param propertyFileName
     * @return
     */
    @SneakyThrows
    public static List<String> getProperty(String propertyFileName){
        return getProperty(propertyFileName,Function.identity());
    }

    /**
     * 读取属性
     * @param propertyFileName
     * @param converter 转换器
     * @return
     */
    @SneakyThrows
    public static List<String> getProperty(String propertyFileName, Function<String,String> converter){
        ResourceLoader resourceLoader=new DefaultResourceLoader();
        Resource resource = resourceLoader.getResource("/property/" + propertyFileName);
        BufferedReader reader=new BufferedReader(new InputStreamReader(resource.getInputStream()));
        List<String> result=new ArrayList<>();
        String line;
        while(StringUtils.isNotBlank(line=reader.readLine())){
            result.add(converter.apply(line));
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(getProperty("test"));
    }
}
